package com.pianke.yc.test.Net;

import android.os.AsyncTask;

import com.pianke.yc.test.Classess.HttpMethod;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

/**
 * Created by YC on 2016/4/1.
 */
public class NetObject extends AsyncTask<Void, Void, String> {

    private String mUrl;
    public Map<String, String> mParmes;
    private HttpMethod mHttpMethod;
    private SuccessCallback mSuccessCallback;
    private FailCallback mFailCallback;

    public NetObject(String url, Map<String, String> parmes, HttpMethod httpMethod, SuccessCallback successCallback, FailCallback failCallback) {
        mUrl = url;
        mParmes = parmes;
        mHttpMethod = httpMethod;
        mSuccessCallback = successCallback;
        mFailCallback = failCallback;
    }

    private String doPost(String url, Map<String, String> parmes) {
        try {
            URL httpurl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) httpurl.openConnection();
            connection.setRequestMethod("GET");
            connection.setReadTimeout(5000);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            String data = new String();
            for (Map.Entry a : parmes.entrySet()) {
                data += a.getKey() + "=" + a.getValue() + "&";
            }
            data = data.substring(0, data.length() - 1);
            OutputStream out = connection.getOutputStream();
            out.write(data.getBytes());
            out.close();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String str = null;
            StringBuffer sb = new StringBuffer();
            while ((str = reader.readLine()) != null)
                sb.append(str);
            return sb.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String doGet(String url, Map<String, String> parmes) {
        try {
            String data = new String();
            for (Map.Entry a : parmes.entrySet()) {
                data += a.getKey() + "=" + a.getValue() + "&";
            }
            data = data.substring(0, data.length() - 1);
            url = url + "?" + data;
            URL httpurl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) httpurl.openConnection();
            connection.setRequestMethod("GET");
            connection.setReadTimeout(5000);
            connection.setDoInput(true);
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String str = null;
            StringBuffer sb = new StringBuffer();
            while ((str = reader.readLine()) != null)
                sb.append(str);
            return sb.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected String doInBackground(Void... params) {
        String json=null;
        switch (mHttpMethod) {
            case POST:
                json = doPost(mUrl, mParmes);
                break;
            case GET:
                json = doGet(mUrl, mParmes);
                break;
        }
        return json;
    }

    @Override
    protected void onPostExecute(String s) {
        if (s != null) {
            if (mSuccessCallback != null) {
                mSuccessCallback.onSuccess(s);
            }
        } else {
            if (mFailCallback != null) {
                mFailCallback.onFail();
            }
        }
    }


    public interface SuccessCallback {
        void onSuccess(String a);
    }

    public interface FailCallback {
        void onFail();
    }


}
